package com.jrx.anytxn.param.controller.system;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.system.CodeTableRes;
import com.jrx.anytxn.param.dto.system.CodeTableTypeReq;
import com.jrx.anytxn.param.dto.system.CodeTableTypeRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.system.ICodeTableService;
import com.jrx.anytxn.param.service.system.ICodeTableTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by yf
 * @Classname CodeTableController
 * @Description 代码表
 * @Date 2019/6/13 0013 下午 13:52
 */
@Api(tags = "代码表参数api接口")
@RestController
@RequestMapping("/param/sysCodes")
public class CodeTableController {

    private static final Logger logger= LoggerFactory.getLogger(CodeTableController.class);

    @Autowired
    private ICodeTableService iCodeTableService;
    @Autowired
    private ICodeTableTypeService iCodeTableTypeService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID通过字典类型ID获取字典列表，按分类ID分组")
    @GetMapping(value = "/getMap")
    public TxnRespResult<HashMap<String, List<CodeTableRes>>> getCodeTableByTypeIds(@RequestParam List<String> typeIds) {
        TxnRespResult<HashMap<String, List<CodeTableRes>>> result = new TxnRespResult<>();
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            Map<String, List<CodeTableRes>> sysCodeMap = iCodeTableService.findMapByTypeIds(typeIds,tenantId);
            result.getSuccess(Maps.newHashMap(sysCodeMap));
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询代码表类型信息", notes = "根据租户ID分页查询代码表类型信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<CodeTableTypeRes>> getAllCodeTableByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,
                                                                          @RequestParam("typeId") String typeId,@RequestParam("typeName") String typeName,@RequestParam("typeDescription") String typeDescription) {
        TxnRespResult<TxnPage<CodeTableTypeRes>> result = new TxnRespResult<>();
        TxnPage<CodeTableTypeRes> res;
        try {
            String tenantId =String.valueOf(infoService.getUser().getProjectId());
            res = iCodeTableTypeService.findAllPageByTenId(pageNum, pageSize, tenantId,typeId,typeName,typeDescription);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增代码类型信息", notes = "新增代码类型信息")
    @PostMapping("/")
    public TxnRespResult<CodeTableTypeRes> createCodeTableType(@RequestBody CodeTableTypeReq codeTableTypeReq) {
        TxnRespResult<CodeTableTypeRes> result = new TxnRespResult<>();
        CodeTableTypeRes response;
        try {
            codeTableTypeReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            response = iCodeTableTypeService.addCodeTable(codeTableTypeReq,infoService.getUser().getUserName());
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑代码类型信息", notes = "编辑代码类型信息")
    @PutMapping("/")
    public TxnRespResult<CodeTableTypeRes> editCodeTableType(@RequestBody CodeTableTypeReq codeTableTypeReq) {
        TxnRespResult<CodeTableTypeRes> result = new TxnRespResult<>();
        CodeTableTypeRes response;
        try {
            response = iCodeTableTypeService.modifyCodeTable(codeTableTypeReq,infoService.getUser().getUserName());
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据主键typeId查询代码类型信息", notes = "根据主键typeId查询代码类型信息")
    @GetMapping(value = "/typeId/{typeId}/tenantId/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "类型ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租戶id", required = true)
    })
    public TxnRespResult<CodeTableTypeRes> getByTypeId(@PathVariable String typeId, @PathVariable String tenantId) {
        TxnRespResult<CodeTableTypeRes> result = new TxnRespResult<>();
        CodeTableTypeRes res;
        try {
            res = iCodeTableTypeService.findByTypeIdAndTenId(typeId, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "删除代码类型信息", notes = "删除代码类型信息")
    @DeleteMapping(value = "/typeId/{typeId}/tenantId/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "类型ID", required = true),
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true)
    })
    public TxnRespResult<Boolean> cancleByTypeId(@PathVariable String typeId, @PathVariable String tenantId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iCodeTableTypeService.removeByTypeIdAndTenId(typeId, String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }


}
